/**
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 */
package com.ce.pms.common.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.springframework.util.Assert;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.dao.IOrganizationalStructureDao;
import com.ce.pms.common.model.HmDeptDomain;
import com.ce.pms.common.model.HmEmployeeDomain;
import com.ce.pms.common.service.IOrganizationalStructureService;
import com.ce.pms.common.vo.HmDeptVO;
import com.ce.pms.common.vo.OrganizationalStructureVO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述: OrganizationalStructureServiceImpl 组织架构业务处理类
 *
 * @author  许心炜
 * Created on：  2008-10-7
 * @author  许心炜
 * update on：  2008-10-29
 */
public class OrganizationalStructureServiceImpl extends PmsBaseServiceImpl implements IOrganizationalStructureService {

	/**
	 * 组织架构DAO注入
	 */
	private IOrganizationalStructureDao organizationalStructureDao;

	/**
	 * 功能描述: 根据部门ID查询部门名字 <br>
	 * @param deptId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-22  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String getDeptNameById(Long deptId){
		Assert.notNull(deptId);
		return this.organizationalStructureDao.getDeptNameById(deptId);
	}

	/**
	 * 功能描述: 根据部门ID查询部门实体 <br>
	 * @param deptId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-22  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public HmDeptDomain getHmDeptDomainById(Long deptId){
		return this.organizationalStructureDao.getHmDeptDomainById(deptId);
	}
	/**
	 * 功能描述:通过员工ID获取员工domain<br>
	 * @param employeeId
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	public HmEmployeeDomain getHmEmployeeDomainById(Long employeeId){
		return organizationalStructureDao.getHmEmployeeDomainById(employeeId);
	}

	/**
	 * @return organizationalStructureDao
	 */
	public IOrganizationalStructureDao getOrganizationalStructureDao() {
		return organizationalStructureDao;
	}

	/**
	 * 功能描述:  查询员工信息列表
	 * @param organizationalStructureVO
	 * @return
	 * Create author:许心炜
	 * Create on:2008-10-7
	 */
	public Page queryOutUserInfo(OrganizationalStructureVO organizationalStructureVO) {
		// TODO Auto-generated method stub
		Page page = organizationalStructureDao.queryOutUserInfo(organizationalStructureVO);
		page.setResult(organizationalStructureDoToVO(page.getResult()));
		return page;
	}

	/**
	 * 功能描述:  根据userCode查询员工信息
	 * @param userCode
	 * @return
	 * Create author:许心炜
	 * Create on:2008-10-29
	 */
	public OrganizationalStructureVO queryUserByUserCode(String userCode) {
		// TODO Auto-generated method stub
		HmEmployeeDomain hmEmployeeDomain = organizationalStructureDao.queryUserByUserCode(userCode);

		return hmEmployeeDomain == null ? null : organizationalStructureDoToVO(hmEmployeeDomain);
	}

	/**
	 * @param organizationalStructureDao the organizationalStructureDao to set
	 */
	public void setOrganizationalStructureDao(
			IOrganizationalStructureDao organizationalStructureDao) {
		this.organizationalStructureDao = organizationalStructureDao;
	}

	/**
	 * 功能描述:转换单个domain为VO<br>
	 * @param domain
	 * @return
	 * Create author:许心炜 <br>
	 * Create on:2008-10-7  <br>
	 */
	private OrganizationalStructureVO organizationalStructureDoToVO(HmEmployeeDomain domain){
		if(domain == null) return null;
		OrganizationalStructureVO vo = new OrganizationalStructureVO();
		if(domain.getDeptDO() != null){//设置部门名称
			vo.setDept(domain.getDeptDO().getDeptName());
			vo.setDeptId(domain.getDeptDO().getDeptId());
		}
		vo.setEmployeeName(domain.getEmployeeName());
		vo.setEmplyeeId(domain.getEmployeeId());
		if(!BlankUtil.isBlank(domain.getSesUser())){
			vo.setUserCode(domain.getSesUser().getUserCode());
			vo.setUserId(domain.getSesUser().getUserId());
		}
		else{
			vo.setUserCode(null);
			vo.setUserId(null);
		}
		return vo;

	}


	/**
	 * 功能描述:批量将Domain转换为VO<br>
	 * @param domainlist
	 * @return
	 * Create author:许心炜 <br>
	 * Create on:2008-10-7  <br>
	 */
	private List<OrganizationalStructureVO> organizationalStructureDoToVO(List<HmEmployeeDomain> domainlist){
		if(domainlist == null || domainlist.size() == 0) return null;
		List<OrganizationalStructureVO> voList = new ArrayList<OrganizationalStructureVO>();
		for(Iterator<HmEmployeeDomain> iterator = domainlist.iterator(); iterator.hasNext();){
			voList.add(organizationalStructureDoToVO(iterator.next()));
		}
		return voList;
	}

	/**
	 * 功能描述:  根据userID查询部门信息
	 * @param userCode
	 * @return HmDeptVO
	 * Create author:许心炜
	 * Create on:2008-10-29
	 */
	public HmDeptVO queryDeptInfoByUseID(Long userID){
		if(userID == null || BlankUtil.isBlank(userID)){
			return null;
		}

		HmDeptVO hmDeptVO = new HmDeptVO();
		HmDeptDomain deptDomain = organizationalStructureDao.queryDeptInfoByUseID(userID);
		BeanUtil.convertVoAsDo(deptDomain,hmDeptVO);
		return hmDeptVO;
	}

	/**
	 * 功能描述:  根据userID查询员工信息<br>
	 * @param userID
	 * @return OrganizationalStructureVO
	 * Create author:许心炜 <br>
	 * Create on:2008-12-24  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public OrganizationalStructureVO queryUserByUserId(Long userID) {
		// TODO Auto-generated method stub
		if(BlankUtil.isBlank(userID)){
			return null;
		}

		HmEmployeeDomain hmEmployeeDomain = organizationalStructureDao.queryUserByUserId(userID);

		return hmEmployeeDomain == null ? null : organizationalStructureDoToVO(hmEmployeeDomain);
	}

	/**
	 * 功能描述:获得所有部门员工的信息<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-30<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public List<OrganizationalStructureVO> getAllPerson(){
		List<OrganizationalStructureVO> listVO=new ArrayList<OrganizationalStructureVO>();
		List<HmEmployeeDomain> list=organizationalStructureDao.getAllPerson();
		for(HmEmployeeDomain hmEmployeeDomain:list){
			HmDeptVO hmDeptVO=new HmDeptVO();
			listVO.add(organizationalStructureDoToVO(hmEmployeeDomain));
		}
		return listVO;
	}

	/**
	 * 功能描述:获得所有部门的信息<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-30<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public List<HmDeptVO> getAllDept(){

		List<HmDeptVO> listVO=new ArrayList<HmDeptVO>();

		List<HmDeptDomain> list= organizationalStructureDao.getAllDept();
		for(HmDeptDomain deptDomain:list){
			HmDeptVO hmDeptVO = new HmDeptVO();
		   BeanUtil.convertVoAsDo(deptDomain,hmDeptVO);
		    // listVO.add(hmDeptVO.getDeptId().intValue(),hmDeptVO);
		   listVO.add(hmDeptVO);
		}
		return listVO;

	}

	/**
	 * 功能描述:获得所选部门的信息<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-30<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public List<HmDeptVO> getSelectDeptList(String deptIdStr){

		List<HmDeptVO> listVO=new ArrayList<HmDeptVO>();
		if(BlankUtil.isBlank(deptIdStr)){
			return listVO;
		}
		String []deptIdArray=deptIdStr.split(",");
		for(String deptId:deptIdArray){
			HmDeptDomain hmDeptDomain=organizationalStructureDao.queryDeptDoById(Long.parseLong(deptId));
			if(BlankUtil.isBlank(hmDeptDomain)){
				continue;
			}
			HmDeptVO hmDeptVO = new HmDeptVO();
			BeanUtil.convertVoAsDo(hmDeptDomain,hmDeptVO);
			listVO.add(hmDeptVO);
		}

		return listVO;
	}
}
